﻿package com.ufenlei.LTS{
	
	 import flash.display.Sprite;
	 import flash.text.TextField;
	 import flash.events.Event;

	public class TestingItemView extends Sprite{
		private var _ti:TestingItem;
		private var _titleField:TextField;
		private var _og:TestingRadioGroup;
		private var loadedNum:int =0;
		
		public function TestingItemView(ti:TestingItem){
			_ti = ti;
			_titleField = tivtitle;
			_titleField.text = _ti.id + ". "+_ti.question;
		};
		public function showKey():void{
			_og.showKey(_ti.key);
		}
		public function init():void{
			_og = new TestingRadioGroup(_ti.type);
			if(_ti.type == TestingItem.TYPE_IMAGE){
				_og.addEventListener(Event.COMPLETE, onLoadingComplete);
			}
			for each( var op in _ti.optionList){
				_og.addRadio(op.desc , op.key);
				_og.addEventListener(Input.CHANGE, onChange);
			}
			if(_ti.type !== TestingItem.TYPE_IMAGE){
				this.dispatchEvent(new Event(Event.COMPLETE, true));
			}
			this.addChild(_og);
			_og.y = 30;
			_og.x = 8;			
		}
		private function onLoadingComplete(evt:Event):void{
			loadedNum++;
			if(loadedNum == _ti.optionList.length){
				this.dispatchEvent(new Event(Event.COMPLETE , true));
			}
		}
		private function onChange(evt:Event):void{
			_ti.setAnswer((evt.target as TestingRadioGroup).answer);
			var evt1:Event = new Event(Input.CHANGE);
			this.dispatchEvent(evt1);
		}
	 }
}